//
//  SplashScreenView.m
//  ChatDemo-UI2.0
//
//  Created by li.guibin on 15/12/7.
//  Copyright © 2015年 li.guibin. All rights reserved.
//

#import "SplashScreenView.h"

@interface SplashScreenView ()
{
    CGRect scrollFrame;
    NSArray *imageArr;
}

@end

@implementation SplashScreenView
- (instancetype)initWithFrame:(CGRect)frame imageDataArr:(NSArray *)dataArr
{
    self = [super initWithFrame:frame];
    if (self) {
        
        scrollFrame = frame;
        imageArr = dataArr;
        self.isSkip = NO;
        
        if (dataArr) {
            [self creatScrollView];
            
            
        }else {
            [self loadImageDataRequest];
        }
        
       
    }
    self.backgroundColor = [UIColor whiteColor];
    self.pagingEnabled = YES;
    return self;
}

- (void)loadImageDataRequest {
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_startpic_list" forKey:@"method"];
    [dict setObject:@"0" forKey:@"type"];
    if (USERID) {
        
        [dict setObject:USERID forKey:@"userid"];
    }
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        NSArray *dataArr = [[NSArray alloc]initWithArray:[dict objectForKey:@"re_data"]];
        
        self.contentSize = CGSizeMake(self->scrollFrame.size.width * [dataArr count], self->scrollFrame.size.height);
        for ( NSInteger NUM = 0; NUM < [dataArr count]; NUM ++) {
            NSDictionary * subDict = [dataArr objectAtIndex:NUM];
            NSString * pic = [subDict objectForKey:@"pic"];
            NSString * picURL = [NSString stringWithFormat:@"%@%@",IMAGEHOSTURL,pic];
            
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self->scrollFrame.size.width * NUM, 0, self->scrollFrame.size.width, self->scrollFrame.size.height)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:picURL]];
            [self addSubview:imageView];
            if (NUM + 1 == [dataArr count]) {
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToRemove)];
                imageView.userInteractionEnabled = YES;
                [imageView addGestureRecognizer:tap];
            }
            
        }
    } failure:^(NSInteger recode, NSError *error) {
        [self performSelector:@selector(tapToRemove)];
    }];
    
}

- (void)creatScrollView{
    
    self.contentSize = CGSizeMake(self->scrollFrame.size.width * [imageArr count], scrollFrame.size.height);
    for ( NSInteger NUM = 0; NUM < [imageArr count]; NUM ++) {
        
//        NSString * pic = [imageArr objectAtIndex:NUM];
//        NSString * picURL = [NSString stringWithFormat:@"%@%@",IMAGEHOSTURL,pic];
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollFrame.size.width * NUM, 0, scrollFrame.size.width, scrollFrame.size.height)];
        //[imageView sd_setImageWithURL:[NSURL URLWithString:picURL]];
        [imageView setImage:[UIImage imageNamed:[imageArr objectAtIndex:NUM]]];
        [self addSubview:imageView];
        if (NUM + 1 == [imageArr count]) {
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToRemove)];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
        }
        
    }
    
}

- (void)tapToRemove{
    
    if (self.viewWasHide) {
        
        self.viewWasHide();
    }
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
